$(document).ready(function() {
//	top.UppTabs.operator = top.getCurrentTagTitle();// 设置用户tab的标题值
	//doSearch();
});

function openSelector() {
	Boxy.seller($("#sellerIds").val(),contextPath, function(val) {
		var names="";
		var values="";
		if(val!=""){
			var choosed=val.split(",");
			for(var i=0;i<choosed.length;i++){
				var t = choosed[i].split("|");
				names+=t[1]+",";
				values+=t[0]+",";
			}
		}
		$("#sellers").val(names);
		$("#sellerIds").val(values);
	}, {
		title : "商家选择器"
	});
	return false;
}

function doSearch(){
	var s = $('input[name="beginTime"]').val();
	var e = $('input[name="endTime"]').val();
	//if(s == "" || e == "")
	//	return;
	$("#data_list").datagrid({
		onLoadError : function(data) {
			uppAjaxError(data);
		},
		onLoadSuccess: function (data) {
			paint();
		},
		queryParams : {
			beginTime : s,
			endTime : e,
			sellerIds:$("#sellerIds").val()
		},
		columns : [ [ {
			field : 'daliyEarningsDate',
			title : '日期范围 ',
			align : 'center',
			formatter : function(value, row, index) {
				return s+"~"+e;
			}
		}, {
			field : 'merchantName',
			title : '所选商家',
			formatter:function(value,row,index){
				return $("#sellers").val();
			}
		}, {
			field : 'comboCardExpectedEarnings',
			title : '套餐卡预期收益总额'
		}, {
			field : 'rechargeableCardExpectedEarnings',
			title : '充值卡预期收益总额'
		}, {
			field : 'expectedTotalEarningsSum',
			title : '预期收益总额累积'
		} ] ],
		autoRowHeight : true,
		fitColumns : true,
		pagination : true,
		nowrap : false,
		singleSelect : true,
		rownumbers : true
	});
}

function paint(){
	var data=$("#data_list").datagrid("getRows");
	var days = new Array();
	var totals = new Array();
	var packages = new Array();
	var prepaids = new Array();
	var packages_total =0.0;
	var prepaids_total =0.0;
	for(var i = 0;i<data.length;i++){
		var d =new Date(Number(data[i].daliyEarningsDate));
		days[i] = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
		totals[i] = data[i].expectedTotalEarningsSum;
		packages[i] = data[i].comboCardExpectedEarnings;
		prepaids[i] = data[i].rechargeableCardExpectedEarnings;
		packages_total+=data[i].comboCardExpectedEarnings;
		prepaids_total+= data[i].rechargeableCardExpectedEarnings;
	}
    //饼图
    $('#income_rate').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '收益额统计总占比图'
        },
        subtitle: {
            text: '('+days[0]+'至'+days[days.length-1]+')'
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: '占比',
            data: [
                ['购物卡收益',   100*prepaids_total/(prepaids_total+packages_total)],
                ['套餐卡收益',   100*packages_total/(prepaids_total+packages_total)]
            ]
        }]
    });
}